Run Ollama models in Google Sheets
A step-by-step guide to using Deepseek, Llama, Gemma, Phi, Qwen, and other open source models directly within Google Sheets
This tutorial demonstrates how to connect a local Ollama LLM installation directly to Google Sheets using a Cloudflare Tunnel. (Don't worry, it's not as complicated as it seems). The video provides step-by-step instructions for both Mac and Windows machines, including installing Ollama, downloading open source AI models, configuring a Cloudflare Tunnel to securely expose your local Ollama instance, and setting up a simple Google Apps Script to integrate custom AI functions into your spreadsheet.
The advantage of using this sort of setup is that it enables unrestricted, automated AI queries directly in Google Sheets, leveraging any locally-hosted open-source LLM such as DeepSeek-R1, Llama 4, Phi-4, and others. Unlike Google's native Gemini AI function, this approach doesn't impose context limits, supports automatic refreshes, and allows users to maintain full control over their AI model and data privacy.
Want to do AI-Powered Technical Analysis directly inside of Google Sheets?
Check out the FREE AI for Charts Google Sheets Add On: aiforcharts.com
Guide: How to connect Ollama (local LLM) to Google Sheets in under 2 minutes
1. Install Ollama and a model (in terminal (Mac) or command prompt or powershell (Windows)
ollama run deepseek-r1:1.5b
Then terminate the chat process.
2. Start Ollama server
ollama serve
Make sure to have the actual Ollama application/program running too.
3. Expose Ollama via Cloudflare Tunnel (install cloudflared
first)
For Mac:
brew install cloudflared
For Windows:
install Cloudflare.cloudflared
Then for either Mac or Windows
cloudflared tunnel --url http://127.0.0.1:11434 --http-host-header "localhost:11434"
4. Setup Google Sheets
New sheet → new tab called Settings
Settings!A1: Paste your Cloudflare URL (https://....trycloudflare.com)
Settings!A2: Paste model name (
deepseek-r1:1.5b or gemma3:1b
)
5. Add Google Apps Script (Extensions → Apps Script)
const URL_CELL = 'Settings!A1'; // base tunnel URL
const MODEL_CELL = 'Settings!A2'; // cell that holds the model name
function OLLAMA(prompt, model) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
let base = ss.getRange(URL_CELL).getValue().toString().trim();
let mdl = model || ss.getRange(MODEL_CELL).getValue().toString().trim(); // ← one new line
if (!base) throw new Error('Missing tunnel URL in ' + URL_CELL);
if (!mdl) throw new Error('Missing model name in ' + MODEL_CELL);
if (base.endsWith('/')) base = base.slice(0, -1); // strip trailing /
const url = base + '/api/generate';
const r = UrlFetchApp.fetch(url, {
method : 'post',
contentType : 'application/json',
payload : JSON.stringify({ model: mdl, prompt, stream: false })
});
return JSON.parse(r.getContentText()).response;
}
6. Test it
In any cell:
=OLLAMA("What's 2+2?")
7. Reference another cell
=OLLAMA("Is the following news headline positive or negative in sentiment", A3)
Done! You can now run AI prompts directly from Sheets.
Learn more about AI for Charts: aiforcharts.com
Subscribe to the Deep Charts YouTube Channel for more informative AI and Machine Learning Tutorials.